<template>
  <el-dialog
    :title="!dataForm.punchId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="体温是否正常" prop="isNormal">
      <el-radio-group v-model="dataForm.isNormal">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">异常</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="是否出现发热" prop="isHot">
      <el-radio-group v-model="dataForm.isHot">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="是否咳嗽呼吸困难" prop="isCough">
      <el-radio-group v-model="dataForm.isCough">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="最近一周是否去过高风险地区" prop="warmCorner">
      <el-radio-group v-model="dataForm.warmCorner">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
    </el-form-item>
    <el-form-item label="健康码是否正常" prop="healthCode">
      <el-radio-group v-model="dataForm.healthCode">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          punchId: 0,
          userId: '',
          punchDate: '',
          punchTime: '',
          punchStatus: '',
          punchLocation: '',
          isNormal: '',
          isHot: '',
          isCough: '',
          warmCorner: '',
          healthCode: ''
        },
        dataRule: {
          isNormal: [
            { required: true, message: '体温是否正常不能为空', trigger: 'blur' }
          ],
          isHot: [
            { required: true, message: '是否出现发热不能为空', trigger: 'blur' }
          ],
          isCough: [
            { required: true, message: '是否咳嗽呼吸困难不能为空', trigger: 'blur' }
          ],
          warmCorner: [
            { required: true, message: '最近一周是否去过高风险地区不能为空', trigger: 'blur' }
          ],
          healthCode: [
            { required: true, message: '健康码是否正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.punchId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.punchId) {
            this.$http({
              url: this.$http.adornUrl(`/student/punch/info/${this.dataForm.punchId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.punch.userId
                this.dataForm.punchDate = data.punch.punchDate
                this.dataForm.punchTime = data.punch.punchTime
                this.dataForm.punchStatus = data.punch.punchStatus
                this.dataForm.punchLocation = data.punch.punchLocation
                this.dataForm.isNormal = data.punch.isNormal
                this.dataForm.isHot = data.punch.isHot
                this.dataForm.isCough = data.punch.isCough
                this.dataForm.warmCorner = data.punch.warmCorner
                this.dataForm.healthCode = data.punch.healthCode
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/headmaster/punch/passPunch`),
              method: 'post',
              data: this.$http.adornData({
                'punchId': this.dataForm.punchId || undefined,
                'userId': this.dataForm.userId,
                'punchDate': this.dataForm.punchDate,
                'punchTime': this.dataForm.punchTime,
                'punchStatus': this.dataForm.punchStatus,
                'punchLocation': this.dataForm.punchLocation,
                'isNormal': this.dataForm.isNormal,
                'isHot': this.dataForm.isHot,
                'isCough': this.dataForm.isCough,
                'warmCorner': this.dataForm.warmCorner,
                'healthCode': this.dataForm.healthCode
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
